<?php
require_once("../inc/function.php");

function GetContactInfo()
{
        $conn = GetConnection();
        return $conn->query('SELECT * FROM ContactInfo');
}

function GetContactInfos($id)
{
        $conn = GetConnection();
		if($id==NULL)
			die("Contact couldn't be found with provided id or id is invalid. Please enter another id.");			
        $sql = "SELECT * FROM ContactInfo WHERE id=$id";
        $results = $conn->query($sql);        
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}

function SaveContactInfo($row)
{	    
		$conn = GetConnection();
		$id = $row['id'];
		$customer = strip_tags($row['customer_id']);
		$keyword = strip_tags($row['keyword_id']);
		$contact = strip_tags($row['contact']);
		if(strlen($contact)>0)
		{
			   $conn->query("UPDATE `ContactInfo` "
				.	"SET contact='$contact', Contact_keywords_id='$keyword', Customer_id = '$customer' "
				.	"WHERE id='$id' ");        
		}
		else {
			header("Location: ../ContactInfo/edit.php");
		}		  
        $conn->close();
		
        return $error == '' ? true : array('SQL error' => $error);	
}

function NewContactInfo($row)
{
	$customer = $row['customer_id'];
	$keyword = $row['keyword'];
	$value = strip_tags($row['value']);
	if(strlen($value) > 0)
	{
		$conn = GetConnection();
		$conn->query("INSERT INTO ContactInfo Values(NULL, '$value', '$keyword', '$customer')");
		$conn->close();
	}
	else 
	{
		die("You need to enter a keyword value");	
	}
}

function DeleteContactInfo($id)
{
	$conn = GetConnection();
	$results = $conn->query("DELETE FROM ContactInfo WHERE id=$id");
	header('Location: http://cs.newpaltz.edu/~N01852322/Final2/ContactInfo/');
}

function GetContactKeywords()
{
        $conn = GetConnection();
        return $conn->query('SELECT * FROM Contact_keywords');
}

function GetCustomers()
{
        $conn = GetConnection();
        return $conn->query('SELECT * FROM Customer');
}
?>